<?php

use yii\helpers\Url;

$this->context->layout = false;
?>
<?php
//使用率
echo $this->render("@app/views/public/useRate.php")
?>
<div id="Summary-Management-List"></div>
<script type="text/x-kendo-template" id="SummaryManagementTemplate">
    <button type="button" data-command="create" class="k-button k-button-icontext k-grid-add"><span
                class="k-icon k-add"></span>新增
    </button>
    <a class="k-button k-button-icontext" onclick="onButtonAuth()"><span class="glyphicon glyphicon-cog"></span>部门类别</a>
    <a class="k-button k-button-icontext" onclick="onLeaderAuth()"><span class="glyphicon glyphicon-cog"></span>代理组长</a>
    <a class="k-button k-button-icontext" onclick="onNotCommented()"><span class="glyphicon glyphicon-cog"></span>未评五大类</a>
    <a class="k-button k-button-icontext" onclick="onHistoryRecord()"><span class="glyphicon glyphicon-cog"></span>查看历史</a>
    <a class="k-button k-button-icontext" onclick="onSummaryMaintain()"><span class="glyphicon glyphicon-cog"></span>查看总汇</a>
    <a class="k-button k-button-icontext btn btn-info btn-lg active"><span class="glyphicon glyphicon-cog"></span>总汇管理层</a>
    <a class="k-button k-button-icontext" onclick="onRatingSettings()"><span class="glyphicon glyphicon-cog"></span>评级设置</a>
    <a class="k-button k-button-icontext" onclick="onAuthManagement()"><span class="glyphicon glyphicon-cog"></span>权限管理</a>
    <a class="k-button k-button-icontext" onclick="onAffectPrincipal()"><span class="glyphicon glyphicon-cog"></span>被影響人負責人</a>
</script>
<script>
    var staffData;
    $("#Summary-Management-List").kendoGrid({
        dataSource: {
            transport: {
                read: function (e) {
                    $.ajax({
                        url: "<?=Url::to(['readsummarymanagement']); ?>",
                        dataType: "json",
                        success: function (redata) {
                            e.success(redata);
                        }
                    })
                },
                create: function (e) {
                    $.ajax({
                        url: "<?=Url::to(['createsummarymanagement'])?>",
                        dataType: "json",
                        type: "POST",
                        data: e.data,
                        success: function (redata) {
                            if (redata.status == 1) {
                                gridRefresh('Summary-Management-List');
                            } else {
                                layer.msg(redata.msg, {icon: 5});
                            }
                        }
                    });
                },
                destroy: function (e) {
                    $.ajax({
                        url: "<?=Url::to(['destroysummarymanagement'])?>",
                        dataType: "json",
                        type: "POST",
                        data: e.data,
                        success: function (redata) {
                            if (redata.status != 1) {
                                layer.msg(redata.msg, {icon: 5});
                            }
                            gridRefresh('Summary-Management-List');
                        }
                    })
                }
            },
            schema: {
                model: {
                    id: "id",
                    fields: {
                        id: {field: "id", editable: false, nullable: true},
                        cn_name: {field: "cn_name"},
                        username: {field: "username"}
                    }
                }
            },
            pageSize: 15
        },
        sortable: true,
        editable: "popup",
        filterable: {
            extra: false,
            operators: {
                string: {
                    contains: "包含",
                    doesnotcontain: "不包含",
                    eq: "等于",
                    neq: "不等于"
                }
            }
        },
        toolbar: kendo.template($("#SummaryManagementTemplate").html()),
        pageable: {
            refresh: true,
            pageSizes: [15, 30, 45, "all"],
            buttonCount: 5
        },
        columns: [
            {
                field: "cn_name",
                title: "部门",
                width:"15%",
                editor: changeDepartment,
                headerAttributes: alignCenterAttribute,
                attributes: alignCenterAttribute
            },
            {
                field: "username",
                title: "姓名",
                width:"15%",
                editor: changeStaff,
                headerAttributes: alignCenterAttribute,
                attributes: alignCenterAttribute
            },
            {
                title: "操作",
                command: ["destroy"]
            }
        ]
    });

    function changeDepartment(container, options) {
        $('<select required data-bind="value:cn_name"></select>')
            .appendTo(container)
            .kendoDropDownList({
                filter: "contains",
                dataTextField: "name",
                dataValueField: "id",
                dataSource: {
                    transport: {
                        read: {
                            url: "<?=Url::to(['getdepartment'])?>",
                            dataType: "json"
                        }
                    }
                },
                change: function (e) {
                    var selectIndex = e.sender.selectedIndex;
                    $.ajax({
                        url: "<?=Url::to(['getdepartmentstaffs']); ?>",
                        data: {'department': e.sender.dataSource._data[selectIndex].id},
                        dataType: "json",
                        success: function (rData) {
                            $("#username").data("kendoDropDownList").select(-1);
                            $("#username").data("kendoDropDownList").setDataSource(rData);
                        }
                    })
                }
            });
    }

    function changeStaff(container, options) {
        $('<select id="username" required data-bind="value:username"></select>')
            .appendTo(container)
            .kendoDropDownList({
                filter: "contains",
                dataTextField: "name",
                dataValueField: "id",
                dataSource: []
            });
    }
</script>